<?php
/**
 * Created by PhpStorm.
 * User: yepanpan
 * Date: 2017/6/26
 * Time: 17:22
 */

namespace Todo\Util;


class Crawler
{
    protected $_config = array();
    protected $_error = false;
    public function __construct($config){
        $this->_config = $config;
    }

    /**
     * 解析内容
     * @param $url
     * @return array|bool
     */
    public function parseContent($url){
        $content = file_get_contents($url);
        if(!$content){
            $this->_error = true;
            return false;
        }
        $dd = new \DOMDocument();
        $dd->loadHTML($content);
        $dom = new \DOMXPath($dd);

        $ret = array();
        foreach ($this->_config['content'] as $k => $config){
            $elements = $dom->query($config['xpath']);
            $ret[$k] =  $dd->saveHTML($elements->item(0));
        }
        if($this->_config['file']){
            $elements = $dom->query($this->_config['file']['xpath']);
            foreach ($elements as $el){
                $ret['files'][] = array(
                    'title' => $el->textContent,
                    'url'   => $this->_config['file']['prefix'].$el->getAttribute($this->_config['file']['attr'])
                );
                //echo $dd->saveHTML($el->parentNode);
            }
        }

        return $ret;
    }

    /**
     * 解析列表页面，返回内容页
     * @param $url
     * @return array|bool
     */
    public function parseList($url){
        $content = file_get_contents($url);
        if(!$content){
            $this->_error = true;
            return false;
        }
        $dd = new \DOMDocument();
        $dd->loadHTML($content);
        $dom = new \DOMXPath($dd);
        $elements = $dom->query($this->_config['list']['xpath']);
        if (is_null($elements)) {
            $this->_error = true;
            return false;
        }
        $list = array();
        foreach ($elements as $element) {
            $list[] =  $this->_config['list']['prefix']. $element->getAttribute('href');
        }
        return $list;
    }

    /**
     * 生成列表页面数据
     * @return array
     */
    public function generateList(){
        $list = array();
        foreach ($this->_config['list']['source'] as $l){
            if(is_array($l)){
                for($i=$l[1]; $i<$l[2];$i++) $list[] = str_replace('{page}',$i, $l[0]);
            }else{
                $list[] = $l;
            }
        }
        return $list;
    }

    /**
     * 解析爬取的table元素里的数据
     * @param string $table html列表
     * @return array|bool
     */
    public function parseTable($table){
        $trs = explode('<tr>', $table);
        if(count($trs) < 2) return false;
        unset($trs[0]);
        $param = array();
        foreach ($trs as $key => $tr){
            $tds = explode('</td>', $tr);
            foreach ($tds as $k=> &$td) {
                $td =trim(strip_tags($td));
                if($td == '') unset($tds[$k]);
            }
            if(count($tds) > 1) {
                $param[]  = array(
                    'title' => $tds[0],
                    'value' => $tds[1]
                );
            }
        }
        return $param;
    }

    /**
     * 简单清除样式，主要去除注释、高度、宽度
     * @param $content
     * @return mixed
     */
    public function simpleStrip($content){
        $content = preg_replace("/<!--[^>]*-->/i", "", $content);//注释内容  上海SEO顾问
        $content = preg_replace("/width=.+?['|\"]/i",'',$content);//去除样式
        $content = preg_replace("/height=.+?['|\"]/i",'',$content);//去除样式
        return $content;
    }

    /**
     * 清除常见样式和属性
     * @param $content
     * @return mixed
     */
    public function allStrip($content){
        $content = preg_replace("/<a[^>]*>/i", "", $content);
        $content = preg_replace("/<\/a>/i", "", $content);
        $content = preg_replace("/<div[^>]*>/i", "", $content);
        $content = preg_replace("/<\/div>/i", "", $content);
        $content = preg_replace("/<!--[^>]*-->/i", "", $content);//注释内容  上海SEO顾问
        $content = preg_replace("/style=.+?['|\"]/i",'',$content);//去除样式
        $content = preg_replace("/class=.+?['|\"]/i",'',$content);//去除样式
        $content = preg_replace("/id=.+?['|\"]/i",'',$content);//去除样式
        $content = preg_replace("/lang=.+?['|\"]/i",'',$content);//去除样式
        $content = preg_replace("/width=.+?['|\"]/i",'',$content);//去除样式
        $content = preg_replace("/height=.+?['|\"]/i",'',$content);//去除样式
        $content = preg_replace("/border=.+?['|\"]/i",'',$content);//去除样式
        $content = preg_replace("/face=.+?['|\"]/i",'',$content);//去除样式
        $content = preg_replace("/face=.+?['|\"]/",'',$content);//去除样式 只允许小写 正则匹配没有带 i 参数
        return $content;
    }
}